package com.gy.def.controller;

import com.gy.def.dataobject.CourseTable;
import com.gy.def.dataobject.GradeTable;
import com.gy.def.dataobject.UserTable;
import com.gy.def.enums.ResultEnum;
import com.gy.def.exception.GySchoolException;
import com.gy.def.service.GradeService;
import com.gy.def.service.UserService;
import com.gy.def.utils.ResultVoUtil;
import com.gy.def.viewobject.PageMap;
import com.gy.def.viewobject.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@CrossOrigin // 允许跨域
@Api(value = "GradeController", description = "成绩录入模块")
@RestController
@RequestMapping("/grade")
public class GradeController {

  @Autowired
  private GradeService gradeService;

  @Autowired
  private UserService userService;

  @ApiOperation(value = "录入成绩", notes = "录入成绩")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "gradeName", value = "科目名", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradeYear", value = "科目学年", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradeUser", value = "科目用户id", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradeSemester", value = "科目学期", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradePoint", value = "科目分数", required = true, dataType = "int")
  })
  @PostMapping("/enterGrade")
  private ResultVo enterGrade(@RequestParam("gradeName") String gradeName,
                              @RequestParam("gradeYear") String gradeYear,
                              @RequestParam("gradeUser") String gradeUser,
                              @RequestParam("gradeSemester") String gradeSemester,
                              @RequestParam("gradePoint") int gradePoint) {
    UserTable ut = userService.findByUserName(gradeUser);
    if (ut == null) {
      throw new GySchoolException(ResultEnum.GetUserAccountNULL);
    }

    GradeTable gt = new GradeTable();
    gt.setGradeName(gradeName);
    gt.setGradePoint(gradePoint);
    gt.setGradeSemester(gradeSemester);
    gt.setGradeUser(ut.getUserId());
    gt.setGradeYear(gradeYear);

    GradeTable result = gradeService.save(gt);

    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }

    return ResultVoUtil.success();
  }

  @ApiOperation(value = "获取成绩列表", notes = "获取成绩列表")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "page", value = "第几页", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "一页有多少个数据", required = true, dataType = "string"),
  })
  @PostMapping("/getGrade")
  private ResultVo<PageMap<GradeTable>> getGrade(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                                 @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize,
                                                 @RequestParam("gradeUser") String gradeUser) {
    UserTable ut = userService.findByUserName(gradeUser);
    if (ut == null) {
      throw new GySchoolException(ResultEnum.GetUserAccountNULL);
    }

    Page<GradeTable> mapList;
    PageRequest pageRequest = new PageRequest(page, pageSize);
    mapList = gradeService.findByGradeUser(gradeUser, pageRequest);

    PageMap<GradeTable> pm = new PageMap<>();
    pm.setNumber(mapList.getNumber());
    pm.setResultList(mapList.getContent());
    pm.setSize(mapList.getSize());
    pm.setTotalElements(mapList.getTotalElements());
    pm.setTotalPages(mapList.getTotalPages());

    return ResultVoUtil.success(pm);
  }

  @ApiOperation(value = "修改成绩", notes = "修改成绩")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "gradeName", value = "科目名", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradeYear", value = "科目学年", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradeSemester", value = "科目学期", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "gradePoint", value = "科目分数", required = true, dataType = "int"),
    @ApiImplicitParam(paramType = "query", name = "gradeId", value = "成绩id", required = true, dataType = "string")
  })
  @PostMapping("/modifyGrade")
  private ResultVo modifyGrade(@RequestParam("gradeName") String gradeName,
                               @RequestParam("gradeYear") String gradeYear,
                               @RequestParam("gradeSemester") String gradeSemester,
                               @RequestParam("gradePoint") int gradePoint,
                               @RequestParam("gradeId") String gradeId) {
    GradeTable gt = gradeService.findByGradeId(gradeId);
    if(gt == null){
      throw new GySchoolException(ResultEnum.ErrorGetGradeNULL);
    }

    gt.setGradeName(gradeName);
    gt.setGradePoint(gradePoint);
    gt.setGradeSemester(gradeSemester);
    gt.setGradeYear(gradeYear);

    return ResultVoUtil.success();
  }
}
